package com.changgou.goods.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.changgou.common.entity.HttpRespond;
import com.changgou.common.enums.HttpStateEnum;
import com.changgou.common.exception.ChangGouException;
import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public HttpRespond<List<Brand>> listBrand() {
        return new HttpRespond<>(HttpStateEnum.SUCCESS, this.brandMapper.selectAll());
    }

    @Override
    public HttpRespond<Brand> findById(Integer id) {
        return new HttpRespond<>(HttpStateEnum.SUCCESS, this.brandMapper.selectByPrimaryKey(id));
    }

    @Override
    public HttpRespond<Void> add(Brand brand) {
        int i = this.brandMapper.insertSelective(brand);
        if(i == 0){
            throw new ChangGouException(HttpStateEnum.INSERT_ERROR);
        }
        return new HttpRespond<>(HttpStateEnum.SUCCESS);
    }

    @Override
    public HttpRespond<Void> update(Brand brand) {
        int i = this.brandMapper.updateByPrimaryKeySelective(brand);
        if(i == 0){
            throw new ChangGouException(HttpStateEnum.UPDATE_ERROR);
        }
        return new HttpRespond<>(HttpStateEnum.SUCCESS);
    }

    @Override
    public HttpRespond<Void> delete(Integer id) {
        int i = this.brandMapper.deleteByPrimaryKey(id);
        if(i == 0){
            throw new ChangGouException(HttpStateEnum.DELETE_ERROR);
        }
        return new HttpRespond<>(HttpStateEnum.SUCCESS);
    }

    @Override
    public HttpRespond<List<Brand>> findList(Brand brand) {
        // 构建查询条件
        Example example = createExample(brand);
        List<Brand> brandList = brandMapper.selectByExample(example);
        if(CollectionUtil.isEmpty(brandList)){
            throw new ChangGouException(HttpStateEnum.NOT_FOUND);
        }
        // 根据构建的条件查询数据
        return new HttpRespond<>(HttpStateEnum.SUCCESS, brandList);
    }

    @Override
    public HttpRespond<PageInfo<Brand>> findPage(int page, int size) {
        // 静态分页
        PageHelper.startPage(page,size);
        List<Brand> brands = brandMapper.selectAll();
        if(CollectionUtil.isEmpty(brands)){
            throw new ChangGouException(HttpStateEnum.NOT_FOUND);
        }
        // 分页查询
        return new HttpRespond<>(HttpStateEnum.SUCCESS, new PageInfo<>(brands));
    }

    @Override
    public HttpRespond<PageInfo<Brand>> findPage(Brand brand, int page, int size) {
        // 分页
        PageHelper.startPage(page,size);
        // 搜索条件构建
        Example example = createExample(brand);
        List<Brand> brandList = brandMapper.selectByExample(example);
        if(CollectionUtil.isEmpty(brandList)){
            throw new ChangGouException(HttpStateEnum.NOT_FOUND);
        }
        // 分页查询
        return new HttpRespond<>(HttpStateEnum.SUCCESS, new PageInfo<>(brandList));
    }

    private Example createExample(Brand brand) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(brand != null) {
            // 品牌名称
            if (StringUtils.isNotBlank(brand.getName())) {
                criteria.andLike("name", "%" + brand.getName() + "%");
            }
            // 品牌图片地址
            if (StringUtils.isNotBlank(brand.getImage())) {
                criteria.andLike("image", "%" + brand.getImage() + "%");
            }
            // 品牌的首字母
            if (StringUtils.isNotBlank(brand.getLetter())) {
                criteria.andLike("letter", "%" + brand.getLetter() + "%");
            }
            // 品牌id
            if (StringUtils.isNotBlank(brand.getLetter())) {
                criteria.andEqualTo("id", brand.getId());
            }
            // 排序
            if (brand.getSeq() != null) {
                criteria.andEqualTo("seq", brand.getSeq());
            }
        }
        return example;
    }

    @Override
    public HttpRespond<List<Map>> findListByCategoryName(String categoryName) {
        List<Map> listByCategoryName = this.brandMapper.findListByCategoryName(categoryName);
        if(CollectionUtil.isEmpty(listByCategoryName)){
            throw new ChangGouException(HttpStateEnum.NOT_FOUND);
        }
        return new HttpRespond<>(HttpStateEnum.SUCCESS, listByCategoryName);
    }
}
